 /**
  * Copyright (C) 2021 - 2031 O-Cubes Co., Ltd.
  */

/****************************************************************
 *  @file    security_test.c
 *  @brief   secutity test source file
 *  @version v1.0
 *  @date    03. Apr. 2023
 ****************************************************************/

#include "printf.h"
#include "hal_api.h"
#include "autoconf.h"

static uint8_t test_data[128] = {
	0x32,0xd7,0x25,0xe8,0x35,0x77,0x41,0x99,0x17,0x40,0x68,0x4f,0xaf,0x2b,0xdf,0xf8,
	0xc7,0xaf,0x7b,0xc9,0xbf,0x8b,0xc8,0xc4,0x88,0x17,0xe7,0xdc,0xe6,0xe8,0x0f,0x41,
	0x90,0xb5,0x09,0x83,0x25,0x53,0xee,0x31,0x54,0xc6,0x7c,0x54,0xba,0x2f,0xec,0xc6,
	0x0d,0x82,0x7c,0xcc,0x66,0xa5,0xc4,0x10,0xe8,0x63,0x7c,0x7b,0xb7,0xb6,0x75,0xf2,
	0xae,0x71,0x14,0xc2,0x6a,0x79,0x5b,0x19,0xbf,0x50,0xb2,0x7c,0xc1,0xf5,0x02,0x15,
	0xa2,0x43,0xf8,0x77,0x7f,0xa1,0xbb,0xde,0xdc,0xaf,0xfc,0xca,0xf8,0xca,0x82,0x33,
	0xd4,0xaa,0x08,0xe2,0xef,0xbf,0x7f,0xb9,0x25,0xe3,0xad,0xe1,0x4c,0xab,0x13,0xaa,
	0xca,0xd2,0x09,0x5d,0x89,0x47,0xf4,0x60,0xf3,0xb9,0x34,0xa4,0xbe,0xda,0x2c,0xb2,
};

static uint8_t test_key[16] = {0x17, 0x3e, 0xc4, 0xb6, 0x82, 0x22, 0xce, 0x09, 0xa0, 0xd7, 0x03, 0xaa, 0x23, 0xaa, 0x2f, 0xaf};

static void sec_hw_hk_aes_test(void)
{
	uint8_t hw_key[16] = {0};
	uint8_t hash[32] = {0};
	uint8_t encry_hash[32] = {0};
	uint8_t i = 0;

	Hal_Sec_Sha256(test_data, sizeof(test_data), hash, SEC_OP_HW_SHA256);
	printf("HW HK HASH:             ");
	for (i = 0; i < 32; i++)
		printf("%02x ", hash[i]);
	printf("\r\n");

	Hal_Sec_Hw_Hk_Crypto(hash, 32, hw_key, encry_hash, 0x12345678, SEC_OP_HW_HK_AES_ENC);
	printf("HW HK AES ENCRYPT KEY:  ");
	for (i = 0; i < 16; i++) {
		printf("%02x ", hw_key[i]);
	}
	printf("\r\n");

	printf("HW HK AES ENCRYPT HASH: ");
	for (i = 0; i < 32; i++) {
		hash[i] = 0;
		printf("%02x ", encry_hash[i]);
	}
	printf("\r\n");

	Hal_Sec_Hw_Sk_Crypto(encry_hash, 32, hw_key, SEC_KEY_LEN, hash, SEC_OP_HW_SK_AES_DEC);
	printf("HW HK AES DECRYPT HASH: ");
	for (i = 0; i < 32; i++) {
		encry_hash[i] = 0;
		printf("%02x ", hash[i]);
	}
	printf("\r\n\r\n");
}

static void sec_hw_hk_sm4_test(void)
{
	uint8_t hw_key[16] = {0};
	uint8_t hash[32] = {0};
	uint8_t encry_hash[32] = {0};
	uint8_t i = 0;

	Hal_Sec_Sha256(test_data, sizeof(test_data), hash, SEC_OP_HW_SHA256);
	printf("HW HK HASH:             ");
	for (i = 0; i < 32; i++)
		printf("%02x ", hash[i]);
	printf("\r\n");

	Hal_Sec_Hw_Hk_Crypto(hash, 32, hw_key, encry_hash, 0x78654321, SEC_OP_HW_HK_SM4_ENC);
	printf("HW HK SM4 ENCRYPT KEY:  ");
	for (i = 0; i < 16; i++) {
		printf("%02x ", hw_key[i]);
	}
	printf("\r\n");

	printf("HW HK SM4 ENCRYPT HASH: ");
	for (i = 0; i < 32; i++) {
		hash[i] = 0;
		printf("%02x ", encry_hash[i]);
	}
	printf("\r\n");

	Hal_Sec_Hw_Sk_Crypto(encry_hash, 32, hw_key, SEC_KEY_LEN, hash, SEC_OP_HW_SK_SM4_DEC);
	printf("HW HK SM4 DECRYPT HASH: ");
	for (i = 0; i < 32; i++) {
		encry_hash[i] = 0;
		printf("%02x ", hash[i]);
	}
	printf("\r\n\r\n");
}

static void sec_hw_sk_aes_test(void)
{
	uint8_t hash[32] = {0};
	uint8_t encry_hash[32] = {0};
	uint8_t i = 0;

	Hal_Sec_Sha256(test_data, sizeof(test_data), hash, SEC_OP_HW_SHA256);
	printf("HW SK HASH:             ");
	for (i = 0; i < 32; i++)
		printf("%02x ", hash[i]);
	printf("\r\n");

	Hal_Sec_Hw_Sk_Crypto(hash, 32, test_key, SEC_KEY_LEN, encry_hash, SEC_OP_HW_SK_AES_ENC);
	printf("HW SK AES ENCRYPT HASH: ");
	for (i = 0; i < 32; i++) {
		hash[i] = 0;
		printf("%02x ", encry_hash[i]);
	}
	printf("\r\n");

	Hal_Sec_Hw_Sk_Crypto(encry_hash, 32, test_key, SEC_KEY_LEN, hash, SEC_OP_HW_SK_AES_DEC);
	printf("HW SK AES DECRYPT HASH: ");
	for (i = 0; i < 32; i++) {
		encry_hash[i] = 0;
		printf("%02x ", hash[i]);
	}
	printf("\r\n\r\n");
}

static void sec_hw_sk_sm4_test(void)
{
	uint8_t hash[32] = {0};
	uint8_t encry_hash[32] = {0};
	uint8_t i = 0;

	Hal_Sec_Sha256(test_data, sizeof(test_data), hash, SEC_OP_HW_SHA256);
	printf("HW SK HASH:             ");
	for (i = 0; i < 32; i++)
		printf("%02x ", hash[i]);
	printf("\r\n");

	Hal_Sec_Hw_Sk_Crypto(hash, 32, test_key, SEC_KEY_LEN, encry_hash, SEC_OP_HW_SK_SM4_ENC);
	printf("HW SK SM4 ENCRYPT HASH: ");
	for (i = 0; i < 32; i++) {
		hash[i] = 0;
		printf("%02x ", encry_hash[i]);
	}
	printf("\r\n");

	Hal_Sec_Hw_Sk_Crypto(encry_hash, 32, test_key, SEC_KEY_LEN, hash, SEC_OP_HW_SK_SM4_DEC);
	printf("HW SK SM4 DECRYPT HASH: ");
	for (i = 0; i < 32; i++) {
		encry_hash[i] = 0;
		printf("%02x ", hash[i]);
	}
	printf("\r\n\r\n");
}

#if defined(CONFIG_SDK_SHA256) && defined(CONFIG_SDK_AES)
static void sec_sw_aes_test(void)
{
	uint8_t hash[32] = {0};
	uint8_t encry_hash[32] = {0};
	uint8_t i = 0;

	Hal_Sec_Sha256(test_data, sizeof(test_data), hash, SEC_OP_SW_SHA256);
	printf("SW HASH:                ");
	for (i = 0; i < 32; i++)
		printf("%02x ", hash[i]);
	printf("\r\n");

	Hal_Sec_Sw_Crypto(hash, 32, encry_hash, test_key, SEC_KEY_LEN, SEC_OP_SW_AES_ENC);
	printf("SW AES ENCRYPT HASH:    ");
	for (i = 0; i < 32; i++) {
		hash[i] = 0;
		printf("%02x ", encry_hash[i]);
	}
	printf("\r\n");

	Hal_Sec_Sw_Crypto(encry_hash, 32, hash, test_key, SEC_KEY_LEN, SEC_OP_SW_AES_DEC);
	printf("SW AES DECRYPT HASH:    ");
	for (i = 0; i < 32; i++) {
		encry_hash[i] = 0;
		printf("%02x ", hash[i]);
	}
	printf("\r\n\r\n");
}
#endif

#if defined(CONFIG_SDK_SHA256) && defined(CONFIG_SDK_SM4)
static void sec_sw_sm4_test(void)
{
	uint8_t hash[32] = {0};
	uint8_t encry_hash[32] = {0};
	uint8_t i = 0;

	Hal_Sec_Sha256(test_data, sizeof(test_data), hash, SEC_OP_SW_SHA256);
	printf("SW HASH:                ");
	for (i = 0; i < 32; i++)
		printf("%02x ", hash[i]);
	printf("\r\n");

	Hal_Sec_Sw_Crypto(hash, 32, encry_hash, test_key, SEC_KEY_LEN, SEC_OP_SW_SM4_ENC);
	printf("SW SM4 ENCRYPT HASH:    ");
	for (i = 0; i < 32; i++) {
		hash[i] = 0;
		printf("%02x ", encry_hash[i]);
	}
	printf("\r\n");

	Hal_Sec_Sw_Crypto(encry_hash, 32, hash, test_key, SEC_KEY_LEN, SEC_OP_SW_SM4_DEC);
	printf("SW SM4 DECRYPT HASH:    ");
	for (i = 0; i < 32; i++) {
		encry_hash[i] = 0;
		printf("%02x ", hash[i]);
	}
	printf("\r\n\r\n");
}
#endif

int32_t test_security(void)
{
	printf("\r\n[SECURITY]: TEST START\r\n");

#if defined(CONFIG_SDK_SHA256) && defined(CONFIG_SDK_AES)
	/* software aes/sha256 test */
	sec_sw_aes_test();
#endif

	/* hardware ase/sha256 test, use software key */
	sec_hw_sk_aes_test();

	/* hardware aes/sha256 test, use hardware key */
	sec_hw_hk_aes_test();


#if defined(CONFIG_SDK_SHA256) && defined(CONFIG_SDK_SM4)
	/* software sm4/sha256 test */
	sec_sw_sm4_test();
#endif

	/* hardware sm4/sha256 test, use software key */
	sec_hw_sk_sm4_test();

	/* hardware sm4/sha256 test, use hardware key */
	sec_hw_hk_sm4_test();

	printf("[SECURITY]: TEST END\r\n");

	return 0;
}

